package com.zxy.ziems.server.alarm.controller;

import com.alibaba.fastjson.JSON;
import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.alarm.pojo.dto.AlarmProcessRecordDTO;
import com.zxy.ziems.server.alarm.pojo.dto.AlarmRecordEvent;
import com.zxy.ziems.server.alarm.pojo.param.AlarmCountParam;
import com.zxy.ziems.server.alarm.pojo.param.AlarmRecordPageParam;
import com.zxy.ziems.server.alarm.pojo.param.AlarmRecordPageTypeParam;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmDealCountVO;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmRecordVO;
import com.zxy.ziems.server.alarm.service.AlarmRecordService;
import com.zxy.ziems.server.alarm.service.AlarmTypeService;
import com.zxy.ziems.server.board.constant.BoardConstant;
import com.zxy.ziems.server.vo.ChartVo;
import com.zxy.ziems.server.websocket.CustomMessage;
import com.zxy.ziems.server.websocket.MyCustomWebSocketHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.socket.TextMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * 告警记录
 *
 * @author chdzq
 * @create 2023/4/13
 */
@RestController
@Validated
@RequestMapping("/api")
public class AlarmRecordController {

    @Autowired
    private MyCustomWebSocketHandler myCustomWebSocketHandler;
    @Autowired
    private AlarmTypeService alarmTypeService;

    /**
     * 分页查询告警记录
     *
     * @return
     */
    @GetMapping("/alarm/record/page")
    public PageResult<AlarmRecordVO> page(@Validated AlarmRecordPageParam param) throws Exception {
        AlarmRecordPageTypeParam alarmRecordPageTypeParam = new AlarmRecordPageTypeParam();
        BeanUtils.copyProperties(param, alarmRecordPageTypeParam);

        if (StringUtils.isNotBlank(param.getAlarmTypeId())) {
            alarmRecordPageTypeParam.setAlarmTypeIds(new ArrayList<String>() {
                {
                    this.add(param.getAlarmTypeId());
                }
            });
        }

        return service.pageListBy(alarmRecordPageTypeParam);
    }

    /**
     * 分页查询告警记录（能源告警列表）
     *
     * @return
     */
    @GetMapping("/alarm/record/energy/page")
    public PageResult<AlarmRecordVO> energyPage(@Validated AlarmRecordPageParam param) {

        AlarmRecordPageTypeParam alarmRecordPageTypeParam = new AlarmRecordPageTypeParam();
        BeanUtils.copyProperties(param, alarmRecordPageTypeParam);
        alarmRecordPageTypeParam.setDeviceTypeCodes(BoardConstant.DeviceTypeCodeEnum.getEnergyDeviceTypeCode());

        return service.pageListBy(alarmRecordPageTypeParam);
    }

    /**
     * 分页查询告警记录(气体告警，消防告警)
     *
     * @return
     */
    @GetMapping("/alarm/record/model/page")
    public PageResult<AlarmRecordVO> modelPage(@Validated AlarmRecordPageTypeParam param) {
        return service.pageListBy(param);
    }

    /**
     * 处理告警记录
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/alarm/record/process")
    public void processAlarm(@Validated(value = AlarmProcessRecordDTO.Add.class) @RequestBody AlarmProcessRecordDTO dto) throws Exception {
        service.processAlarmRecord(dto);
    }

    /**
     * 增加告警记录
     * 自己测试用的
     *
     * @param dto 用户post参数
     * @return
     */
    @PostMapping("/alarm/record/add")
    public void addAlarm(@RequestBody AlarmRecordEvent dto) throws Exception {
        service.add(dto);
    }


    /**
     * 安全报警处理统计
     *
     * @param alarmCountParam
     * @return
     */
    @GetMapping("/alarm/record/dealCount")
    public List<AlarmDealCountVO> dealCount(@Validated(value = AlarmCountParam.Select.class) AlarmCountParam alarmCountParam) {
        return service.dealCount(alarmCountParam.getStartDate(), alarmCountParam.getEndDate(), alarmCountParam.getModelTypeId(), alarmCountParam.getQueryType());
    }

    /**
     * 安全报警类型统计
     *
     * @param alarmCountParam
     * @return
     */
    @GetMapping("/alarm/record/typeCount")
    public ChartVo typeCount(@Validated(value = AlarmCountParam.Select.class) AlarmCountParam alarmCountParam) {
        return service.typeCount(alarmCountParam.getStartDate(), alarmCountParam.getEndDate(), alarmCountParam.getModelTypeId(), alarmCountParam.getQueryType());
    }

    /**
     * TODO 稳定后需删除发送消息测试
     *
     * @param msg
     * @return
     */
    @RequestMapping("/sendMessage")
    public JsonResponse<Void> sendMessage(String msg) {
        CustomMessage message = CustomMessage.builder().type("test").data(msg).build();
        TextMessage customWebSocketMessage = new TextMessage(JSON.toJSONString(message));
        myCustomWebSocketHandler.sendMessageToAllUser(customWebSocketMessage);
        return JsonResponse.SUCCESS();
    }


    private AlarmRecordService service;

    @Autowired
    public void setService(AlarmRecordService alarmRecordService) {
        this.service = alarmRecordService;
    }
}

